package com.gfxy.common.portal.service;

import com.gfxy.common.portal.GfxyCommonConstant;
import com.gfxy.common.portal.dao.SysRoleResourcesMapper;
import com.gfxy.common.portal.entity.SysLoginUser;
import com.gfxy.common.portal.entity.SysRoleResources;
import com.gfxy.common.portal.entity.SysRoleResourcesExample;
import com.gfxy.commons.utils.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author: lijixin 2019-05-21
 */
@Service
public class RoleResourceService {

    @Autowired
    private SysRoleResourcesMapper roleResourcesMapper;

    public int delRoleMenuOrBtn(String roleId, Integer type) {
        SysRoleResourcesExample roleResourcesExample = new SysRoleResourcesExample();
        SysRoleResourcesExample.Criteria criteria = roleResourcesExample.createCriteria();
        criteria.andRoleIdEqualTo(roleId);
        criteria.andTypeEqualTo(type);
        return roleResourcesMapper.deleteByExample(roleResourcesExample);
    }

    /**
     * <p>Method: addRoleMenu </p>
     * <p>Description: 新增角色关联菜单权限</p>
     * <p>date: 2018/10/24 13:27 </p>
     *
     * @author zhuyu[zhuyu@boco.com.cn]
     */
    public void addRoleMenu(String roleId,String appIds) {

        String[] appIdStrs = appIds.split(",");
        for(String appId:appIdStrs){
            SysRoleResources roleResources = new SysRoleResources();
            roleResources.setResourceId(appId);
            roleResources.setRoleId(roleId);
            roleResources.setRelId(UUIDUtil.getUUID());
            roleResources.setType(GfxyCommonConstant.RR_TYPE_MENU);
            roleResourcesMapper.insertSelective(roleResources);
        }
    }

    /**
     * 新增角色关联按钮权限
     * @param roleId
     * @param buttonIds
     */
    public void addRoleMenuBtn(String roleId,String buttonIds) {

        String[] appIdStrs = buttonIds.split(",");
        for(String appId:appIdStrs){
            SysRoleResources roleResources = new SysRoleResources();
            roleResources.setResourceId(appId);
            roleResources.setRoleId(roleId);
            roleResources.setRelId(UUIDUtil.getUUID());
            roleResources.setType(GfxyCommonConstant.RR_TYPE_BTN);
            roleResourcesMapper.insertSelective(roleResources);
        }
    }


    /**
     * 新增用户默认角色
     * @param user
     * @return
     */
    public int addDefaultRoleUser(SysLoginUser user) {
        SysRoleResources roleResources = new SysRoleResources();
        roleResources.setResourceId(user.getUserId());
        roleResources.setRoleId(GfxyCommonConstant.DEFAULT_ROLE_ID);
        roleResources.setRelId(UUIDUtil.getUUID());
        roleResources.setType(GfxyCommonConstant.RR_TYPE_BTN);
        return roleResourcesMapper.insertSelective(roleResources);

    }

    public int deleteUserRole(String userId){
        SysRoleResourcesExample roleResourcesExample = new SysRoleResourcesExample();
        roleResourcesExample.createCriteria().andTypeEqualTo(GfxyCommonConstant.RR_TYPE_USER)
                .andResourceIdEqualTo(userId);
        return roleResourcesMapper.deleteByExample(roleResourcesExample);
    }

    public int addUserRoles(String userId, String roleSelectData) {
        int result = 0;
        String[] roleIds = roleSelectData.split(",");
        for(String roleId:roleIds){
            result = result + this.addUserRole(userId,roleId);
        }
        return result;

    }

    public int addUserRole(String userId, String roleId){
        SysRoleResources roleResources = new SysRoleResources();
        roleResources.setResourceId(userId);
        roleResources.setRoleId(roleId);
        roleResources.setRelId(UUIDUtil.getUUID());
        roleResources.setType(GfxyCommonConstant.RR_TYPE_USER);
        return roleResourcesMapper.insertSelective(roleResources);
    }
}
